//+------------------------------------------------------------------+
//| 04.10.2013 voigtTrailing                                         |
//+------------------------------------------------------------------+
#property copyright "voigtTrailing"

#include <stderror.mqh>
#include <stdlib.mqh>


extern string NameFileSound ="expert.wav";  // 
int vt_oldTime;
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+


double pp;



  void simulate(){
        if(Bars < 2){
         return(0);
     }
     if(OrdersTotal() == 0){
            
         int tN = OrderSend(Symbol(),1,0.10,Bid,0,0,0,NULL,5,0,Blue);
           if(tN > 0){
            Alert("Done");
         }else{
          int lastError = GetLastError();
          Alert("error(",lastError,"): ",ErrorDescription(lastError));
       }
     } 
  
  }

  void trailStops() 
  
  {
      //simulate();
      //return(0);
     for(int i=0; i < OrdersTotal(); i++) {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
           if ( OrderSymbol()==Symbol()) {
               trailStop();
           }
        }
     }
  }
  
  void trailStop(){
    double stopLoss;
    bool isInsideBar = False;
    int iOutSideBar;
    int barShift = iBarShift(Symbol(),0,OrderOpenTime(),false);

    stopLoss = Close[barShift];
    
    for(int i = barShift; i > 0; i--){
      if(OrderType() == OP_BUY){
         if(High[i] < Close[i-1]){
           stopLoss = Low[i];
   
         }else{
   
           //Do no change the Entry sl
           if(iOutSideBar != barShift){
               iOutSideBar = i;
               isInsideBar = True;
               stopLoss = Low[i+1];
            }
            
         }
      //sell   
      }else{
         if(Low[i] < Close[i-1]){
           stopLoss = High[i];
   
         }else{
   
           //Do no change the Entry sl
           if(iOutSideBar != barShift){
               iOutSideBar = i;
               isInsideBar = True;
               stopLoss = High[i+1];
            }
            
         }
      
      }
      
    }
      if(stopLoss != OrderStopLoss()){
    
         ModifyStopLoss(stopLoss);
      }
      //ObjectCreate("Stoploss",OBJ_HLINE,0,0,stopLoss);  
      //ObjectSet("Stoploss",OBJPROP_COLOR,Blue);
      //ObjectSet("Stoploss",OBJPROP_STYLE,STYLE_DASH);
      
  }
  

  void ModifyStopLoss(double ldStopLoss) 
  {
   bool fm;
   ldStopLoss = setStopLose(ldStopLoss, OrderType());
   if(ldStopLoss == OrderStopLoss()){
      return (0);
   }
   


   
   fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,Blue);
   if (fm) {
      PlaySound(NameFileSound);
      }else{
          int lastError = GetLastError();
          Alert("error(",lastError,"): ",ErrorDescription(lastError));
      }
  }
//+------------------------------------------------------------------+

double setStopLose(double stopLoss, int opTrade){
   int digits = MarketInfo(Symbol(),MODE_DIGITS);
   pp = 1/MathPow(10, digits);

   double StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL);   
   //kauf
   if (opTrade == 0){
      if (Bid - stopLoss < StopLevel*pp){
         stopLoss = Bid - (pp*StopLevel);
      }     
   }else{
      if (Ask - stopLoss < StopLevel*pp){
         stopLoss = Ask + (pp*StopLevel);
      }
   }       
   stopLoss = NormalizeDouble(stopLoss,digits);
   return (stopLoss);
}

